package com.zk.springboot.starter;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;

import com.zk.springboot.interfaces.common.IWebFrame;
import com.zk.springboot.interfaces.common.IWebModule;


public class ApplicationFrame implements IWebFrame {
	private static ApplicationFrame frame = new ApplicationFrame();

	private ArrayList<IWebModule> moduleFactory = new ArrayList<IWebModule>();

	private ApplicationContext applicationContext;
	
	public static ApplicationFrame getFrame() {
		return frame;
	}
	
	public ApplicationContext getContext() {
		return frame.applicationContext;
	}

	public void init(ApplicationContext applicationContext) {
		frame.applicationContext=applicationContext;
//		for(String name:applicationContext.getBeanDefinitionNames()) {
//			System.out.println(applicationContext.getBean(name));
//		}
		String moduleBeans[]=applicationContext.getBeanNamesForType(IWebModule.class);
		for(String name:moduleBeans) {
			IWebModule module = (IWebModule) applicationContext.getBean(name);
			frame.moduleFactory.add(module);
			System.out.println("初始化模块："+name);
			module.initModule(applicationContext,frame);
			System.out.println("初始化模块："+name+"完成");
			}
	}

	@Override
	public IWebModule getModuleByName(String moudel_name) {
		for(IWebModule mod:frame.moduleFactory) {
			if(moudel_name!=null&&moudel_name.equals(mod.appId())) {
				return mod;
			}
		}
		return null;
	}

	@Override
	public List<IWebModule> getAllModule() {
		return frame.moduleFactory;
	}
}
